package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyLikeMapper;
import cn.wolfcode.trip.base.query.LikeQueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 攻略点赞资源过滤器
 * */
@RestController
@RequestMapping("/strategieLikes")
public class StrategyLikeController {
    @Autowired
    private IStrategyLikeService likeService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private StrategyLikeMapper likeMapper;

    /**
     * 保存用户点赞大攻略的信息
     *
     * @param strategyLike
     * @return
     */
    @PostMapping("/{strategy.id}/like")
    public JsonResult saveStrategyLike(StrategyLike strategyLike) {
        likeService.save(strategyLike);
        return new JsonResult();
    }

    /**
     * 保存用户点赞评论的信息
     *
     * @param strategyLike
     * @return
     */
    @PostMapping("/{comment.id}/likeComment")
    public JsonResult saveCommentLike(StrategyLike strategyLike) {
        likeService.saveCommentLike(strategyLike);
        return new JsonResult();
    }

    /**
     * 判断当前用户是否点过该攻略的赞
     *
     * @param strategyId 大攻略的id
     * @return
     */
    @GetMapping("/{strategyId}")
    public JsonResult getUserForLike(@PathVariable Long strategyId) {
        JsonResult result = new JsonResult();
        Boolean type = likeService.getUserForLike(strategyId);
        if (type) {
            return result;
        } else {
            result.mark("亲,点赞失败!");
            return result;
        }
    }

    /**
     * 判断当前用户是否点赞过该攻略
     *
     * @param commentId
     * @return
     */
    @GetMapping("/comment/{commentId}")
    public JsonResult getCommentForLike(@PathVariable Long commentId) {
        JsonResult result = new JsonResult();
        Boolean type = likeService.getCommentForLike(commentId);
        if (type) {
            return result;
        } else {
            result.mark("亲,点赞失败!");
            return result;
        }
    }

    /**
     * 用户取消大攻略点赞
     *
     * @param strategyId
     * @return
     */
    @DeleteMapping("/{strategyId}")
    public JsonResult cancelUserForLike(@PathVariable Long strategyId) {
        likeService.deleteUserForLike(strategyId);
        return new JsonResult();
    }

    /**
     * 用户取消评论点赞
     *
     * @param commentId
     * @return
     */
    @DeleteMapping("/comment/{commentId}")
    public JsonResult deleteUserForLike(@PathVariable Long commentId) {
        likeService.deleteCommentForLike(commentId);
        return new JsonResult();
    }

    /**
     * 根据评论的id去查询该评论下的子评论
     *
     * @param qo
     * @return 子评论的集合
     */
    @GetMapping("/{parentId}/repliesComment")
    public PageInfo<StrategyComment> getReplyCommentByStrategyId(StrategyCatalogQueryObject qo) {
        qo.setOrderBy("st.createTime desc");
        // 获取父评论下的子评论的集合
        PageInfo<StrategyComment> parentComments = commentService.queryCommentsByStrategyId(qo);
        for (StrategyComment ele : parentComments.getList()) {
            StrategyCatalogQueryObject childer = new StrategyCatalogQueryObject();
            childer.setParentId(ele.getId());  // 设置指定查找父亲评论的id
            List<StrategyComment> childerQuery = commentMapper.queryCommentsByStrategyId(childer);
            ele.setChilderList(childerQuery);
            ele.setCommentNums(childerQuery.size());
            int likeNum = likeMapper.selectByCommentIdLike(ele.getId());
            ele.setLikeNum(likeNum);
        }
        return parentComments;
    }

    /**
     * 获取到指定id评论的具体内容
     */
    @GetMapping("/{commentId}/comment")
    public StrategyComment getCommentById(@PathVariable Long commentId) {
        StrategyComment comment = commentService.getCommentById(commentId);
        int like = likeMapper.selectByCommentIdLike(commentId);
        comment.setLikeNum(like);
        return comment;
    }

    /**
     * 获取子评论的内容
     * @param commentId
     * @return
     */
    @GetMapping("/{commentId}/reply")
    public StrategyComment getReplyCommentById(@PathVariable Long commentId) {
        StrategyComment comment = commentService.getReplyCommentById(commentId);
        int like = likeMapper.selectByCommentIdLike(commentId);
        comment.setLikeNum(like);
        return comment;
    }

    /**
     * 获取点赞我的未读消息总数
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}")
    public int getLikeMeNum(@PathVariable Long userId) {
        return likeService.countForLikeMe(userId);
    }

    /**
     * 获取点赞我的具体信息内容的集合
     * @param qo
     * @return 点赞我的用户信息的集合
     */
    @GetMapping("/{userId}/user")
    public PageInfo<StrategyLike> getLikeMeList(LikeQueryObject qo) {
        qo.setOrderBy("st.likeTime desc");
        PageInfo<StrategyLike> listLikeUser = likeService.selectListForLikeUser(qo);
        for(StrategyLike like:listLikeUser.getList()){
            likeMapper.updateLikeState(like.getId());
        }
        return listLikeUser;
    }

    @GetMapping("/replyComment/{userId}")
    public int getCommentMeNum(@PathVariable Long userId) {
        return likeService.countForCommentMe(userId);
    }

    @GetMapping("/{userId}/comments")
    public PageInfo<StrategyComment> getCommentMeList(LikeQueryObject qo) {
        qo.setOrderBy("parent.createTime desc");
        PageInfo<StrategyComment> listForCommentUser = commentService.selectListForCommentUser(qo);
        for(StrategyComment like:listForCommentUser.getList()){
            commentMapper.updateCommentState(like.getId());
        }
        return listForCommentUser;
    }

}
